﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
//using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Site_Extractor_GUI.Properties;

namespace Site_Extractor_GUI
{
	public partial class RulesEditingForm : Form
	{
		public RulesEditingForm()
		{
			InitializeComponent();
			PopulateTree();
		}
		private void PopulateTree()
		{
			var sites = XmlClassHandler<Site[]>.Load(Settings.Default.PathToSitesXML);
			var _sites = new List<Site>(sites);
			TreeNode mainNode = new TreeNode("Sites");
			foreach (Site site in _sites)
			{
				TreeNode node = new TreeNode(site.ToString());
				
				TreeNode domains = new TreeNode("Domains");
				foreach (string domain in site.Domains)
				{
					if (domain == string.Empty) continue;
					domains.Nodes.Add(domain);
				}
				TreeNode ruleSets = new TreeNode("Rule Sets");
				foreach(RuleSet set in site.RuleSets)
				{
					TreeNode setNode = new TreeNode(set.Name);
					foreach (Rule rule in set.Rules)
					{
						setNode.Nodes.Add(RuleToNode(rule));
					}
					ruleSets.Nodes.Add(setNode);
				}
				node.Nodes.Add(domains);
				node.Nodes.Add(ruleSets);
				mainNode.Nodes.Add(node);
			}
			treeView1.Nodes.Add(mainNode);
		}
		private TreeNode RuleToNode(Rule rule)
		{
			TreeNode node = new TreeNode(rule.Name);
			if (rule.Exclusions.Count != 0)
			{
				TreeNode ex = new TreeNode("Exclusions");
				foreach (Rule r in rule.Exclusions)
				{
					ex.Nodes.Add(RuleToNode(r));
				}
				node.Nodes.Add(ex);
			}
			if (rule.Rules.Count != 0)
			{
				TreeNode inner = new TreeNode("Inner Rules");
				foreach (Rule r in rule.Rules)
				{
					inner.Nodes.Add(RuleToNode(r));
				}
				node.Nodes.Add(inner);
			}
			return node;
		}
	}

}
